SRCFOLDERS := src/kernel src/ega src/multiboot
OBJS := src/boot.o
OUTPUT := forkos.elf

CFLAGS := -Isrc/include -g -m32 -march=i386 -std=gnu99 -nostdlib -fno-stack-protector -fno-pic -fno-asynchronous-unwind-tables -O2 -Wall -Wextra
LDFLAGS := -T linker.ld -m elf_i386 -O2 -nostdlib

SRCS += $(wildcard $(addsuffix /*.c,$(SRCFOLDERS)))
OBJS += $(patsubst %.c,%.o,$(SRCS))

elf: $(OBJS)
	ld $(LDFLAGS) $^ -o $(OUTPUT)

src/boot.o: src/boot.asm
	nasm -f elf32 src/boot.asm

%.o: %.c
	gcc $(CFLAGS) -c $^ -o $@

.PHONY: clean
clean:
	rm -rvf **/*.o
	rm -rvf src/**/*.o
	rm -rvf forkos.elf forkos/boot/forkos.elf
	rm -rvf forkos.iso 

.PHONY: rescue
rescue: elf
	cp forkos.elf ./forkos/boot/forkos.elf
	grub-mkrescue forkos -o forkos.iso

.PHONY: qemu
qemu: rescue
	qemu-system-i386 -cdrom forkos.iso

.PHONY: qemu-debug
qemu-debug: rescue
	qemu-system-i386 -s -S -cdrom forkos.iso
